//
//  ZQBaseDetailPage.m
//  jianzhik
//
//  Created by 黄健文 on 2018/8/21.
//  Copyright © 2018年 WKWebView. All rights reserved.
//
#import "UIHelper.h"
#import <MJExtension/MJExtension.h>
#import "ZQBaseDetailPage.h"
#import "ZQBaseDetailModels.h"
#import "ZQBaseResultModels.h"
#import "UIImageView+WebCache.h"

@interface ZQBaseDetailPage ()
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (weak, nonatomic) IBOutlet UILabel *salaryLabel;
/** ZQBaseDetailModels */
@property (nonatomic, strong) ZQBaseDetailModels *detailModels;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *jobCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressDetailLabel;
@property (weak, nonatomic) IBOutlet UILabel *jobDateLabel;
@property (weak, nonatomic) IBOutlet UILabel *jobTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *jobDescLabel;
@property (weak, nonatomic) IBOutlet UIImageView *logoImgView;
@property (weak, nonatomic) IBOutlet UILabel *companyNameLabel;
@property (weak, nonatomic) IBOutlet UIButton *bmButton;

@end

@implementation ZQBaseDetailPage

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"兼职详情";
    [self loadNewTopics];
    self.myScrollView.contentInset = UIEdgeInsetsMake(0, 0,self.bmButton.height + 10 , 0);
}

- (IBAction)bmClick:(id)sender {
    [UIHelper showSuccessMessage:@"投递成功!" afterHideHud:2.f toView:self.view completion:nil];
}

- (void)loadNewTopics
{
    AFHTTPSessionManager *mgf = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"appKey"] = @"QTSHE_IOS_USER";
    parameters[@"deviceId"] = @"FB4D7316-8E5C-4723-8975-6C6040767DFA";
    parameters[@"lat"] = @"31.86163159353237";
    parameters[@"lon"] = @"117.211442967556";
    parameters[@"partJobId"] = @(self.jobId);
    parameters[@"sign"] = @"accf8e39903f2e8a708a87b5470186ba";
    parameters[@"timestamp"] = @"1534832788.557279";
    parameters[@"townId"] = @"98";
    parameters[@"version"] = @"4.13.0";
    // name  url  icon
    [mgf POST:kRequestURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task,   NSDictionary * responseObject) {
        ZQBaseResultModels *model = [ZQBaseResultModels mj_objectWithKeyValues:responseObject];
        if (model.code == 4000) {
            NSLog(@"成功");
        }
        
        if (model.isSuccess) {
           self.detailModels  = [ZQBaseDetailModels mj_objectWithKeyValues:model.data];
            [self setModlesData:self.detailModels];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [UIHelper showErrorWithText:@"与服务器连接失败" inView:self.view];
    }];
}

- (void)setModlesData:(ZQBaseDetailModels *)detailModels
{
    self.titleLabel.text = detailModels.title;
    self.jobCountLabel.text = [NSString stringWithFormat:@"招聘人数:%ld",(long)detailModels.jobCount];
    self.addressDetailLabel.text = detailModels.addressDetail;
   
    self.salaryLabel.attributedText = [self dealWithString:detailModels];
    self.jobDateLabel.text = detailModels.jobDate;
    self.jobTimeLabel.text = detailModels.jobTime;
    
    NSMutableAttributedString *attContentStr = [[NSMutableAttributedString alloc] initWithString:detailModels.jobDesc];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:10];
    [attContentStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attContentStr length])];

    self.jobDescLabel.attributedText = attContentStr;
    self.companyNameLabel.text = detailModels.company.name;
    [self.logoImgView sd_setImageWithURL:[NSURL URLWithString:detailModels.company.logo]];
    
}
     
- (NSAttributedString *)dealWithString:(ZQBaseDetailModels *)detailModels
{
    NSMutableString *originStr = [[NSMutableString alloc] initWithString:detailModels.salary];
    NSRange range = [originStr rangeOfString:@"元"];
    [originStr insertString:@"\n" atIndex:range.location];
    NSString *firstStr = [originStr componentsSeparatedByString:@"\n"].firstObject;
    NSString *middleStr=  [originStr componentsSeparatedByString:@"\n"].lastObject;
    [originStr appendString:@"\n"];
    [originStr appendString:detailModels.clearingForm[@"value"]];
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:originStr];
   
    NSRange firstStrRange = [originStr rangeOfString:firstStr];
    NSRange middleStrRange = [originStr rangeOfString:middleStr];
    NSRange lastStrRange = [originStr rangeOfString:detailModels.clearingForm[@"value"]];
    
    NSDictionary * attrifirstStrBute = @{NSForegroundColorAttributeName:[UIColor orangeColor],NSFontAttributeName:[UIFont systemFontOfSize:44]};
    [attrString addAttributes:attrifirstStrBute range:firstStrRange];
  
    NSDictionary * attrimiddleStrBute = @{NSForegroundColorAttributeName:[UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:12]};
    [attrString addAttributes:attrimiddleStrBute range:middleStrRange];
    
    NSDictionary * attrilastStrBute = @{NSForegroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont systemFontOfSize:16]};
    [attrString addAttributes:attrilastStrBute range:lastStrRange];
    return attrString;
}
//@property (weak, nonatomic) IBOutlet UILabel *salaryLabel;
///** ZQBaseDetailModels */
//@property (nonatomic, strong) ZQBaseDetailModels *detailModels;
//@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//@property (weak, nonatomic) IBOutlet UILabel *jobCountLabel;
//@property (weak, nonatomic) IBOutlet UILabel *addressDetailLabel;
//@property (weak, nonatomic) IBOutlet UILabel *jobDateLabel;
//@property (weak, nonatomic) IBOutlet UILabel *jobTimeLabel;
//@property (weak, nonatomic) IBOutlet UILabel *jobDescLabel;
//@property (weak, nonatomic) IBOutlet UIImageView *logoImgView;
//@property (weak, nonatomic) IBOutlet UILabel *companyNameLabel;
@end
